package com.GZHU.controller.save_info;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@RestController
@Controller
public class Save_model_info {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @GetMapping("/save_model_info")
    public void save_model_info(@RequestParam("model_id") String model_id,
                                @RequestParam("producer_id") String producer_id,
                                @RequestParam("model_category") String model_category,
                                @RequestParam("model_description") String model_description,
                                @RequestParam("model_manual") String model_manual,
                                @RequestParam("model_price") String model_price, HttpServletResponse response) throws IOException {
        int price=0;
        try {
            price=Integer.parseInt(model_price);
        }catch (Exception e){
            System.out.println("model_price input err");
        }
        String str=String.format("VALUES ('%s', '%s', '%s','%s', '%d', '%s')", model_id,producer_id,model_category,model_description,price,model_manual);
        String sql="insert into `model_info` (`model_id`, `producer_id`, `model_category`, `model_description`, `model_price`, `model_manual`) " +str;
        try {
            jdbcTemplate.execute(sql);
            log.info("data save success!");
        }catch (Exception e){
            log.info("data save fail!");
        }
        response.sendRedirect("/data_save.html");
    }
}
